// pages/test.js
const app = getApp();
var utils = require('../../utils/util.js');
var touchDot = 0;//触摸时的原点
var time = 0;// 时间记录，用于滑动时且时间小于1s则执行左右滑动
var interval = "";
var touchMove = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time:0,
    timeSec:0,
    timeMin:0,
    timing:true,
    num:0,
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      jiao:app.d.jiao
    })
    var sucFunc = function (that, res) {
      //console.log(res.data)
      wx.getStorage({
        key: 'record',
        success(res1) {
          if(res1.data.length > 0){
            that.setData({
              // nav: res.data.nav,
              course: res1.data,
              count: res1.data.length
            })
            if (options.cateId > 0) {
              var data = res1.data;
              for (var i = 0; i < data.length; i++) {
                if (data[i].id == options.cateId) {
                  that.setData({
                    num: i
                  })
                }
              }
            }
          }else{
            that.storage(res1.data, that, options)
          }
        },
        fail(res2){
          that.storage(res.data, that, options)
        }
      })
      
    }
    var data = { id: options.id, classId: options.classId,openid:app.d.openid}
    that.setData({
      id :options.id
    })
    utils.wxRequest(that, app.d.path, "Profession/paperquestions", data, sucFunc);
    that.timing(0,that);
    
  },
  storage:function(data,that,options){
    wx.setStorage({
      key: 'record',
      data: data
    })
    wx.getStorage({
      key: 'record',
      success(res) {
        that.setData({
          // nav: res.data.nav,
          course: res.data,
          count: res.data.length
        })
        if (options.cateId) {
          var data = res.data;
          for (var i = 0; i < data.length; i++) {
            if (data[i].id == options.cateId) {
              that.setData({
                num: i
              })
            }
          }
        }
      }
    })
  },
  timing:function(time,that){
    setTimeout(function () {
      that.setData({
        // time: that.data.time + 1,
        timeMin: parseInt(time / 60),
        timeSec: time  % 60
      })
      if(that.data.timing){
        time += 1;
        that.timing(time, that);
      }
    },1000)
  },
  opt:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var data = that.data.course;
    var type = e.currentTarget.dataset.type;
    var cla = e.currentTarget.dataset.cla;
    var name = e.currentTarget.dataset.name;
    for(var i = 0; i < data.length; i++){
      if(i == index){
        
        if(cla == 'xa'){
          if (data[i].xa == name) {
            data[i].xa = ''
          } else {
            data[i].xa = name
            if (type == '单选'){
              data[i].xb = '';
              data[i].xc = '';
              data[i].xd = ''
            }
          }
        }else if(cla == 'xb'){
          if (data[i].xb == name) {
            data[i].xb = ''
          } else {
            data[i].xb = name
            if (type == '单选') {
              data[i].xa = '';
              data[i].xc = '';
              data[i].xd = ''
            }
          }
        }else if(cla == 'xc'){
          if (data[i].xc == name) {
            data[i].xc = ''
          } else {
            data[i].xc = name
            if (type == '单选') {
              data[i].xb = '';
              data[i].xa = '';
              data[i].xd = ''
            }
          }
        }else{
          if (data[i].xd == name) {
            data[i].xd = ''
          } else {
            data[i].xd = name
            if (type == '单选') {
              data[i].xb = '';
              data[i].xc = '';
              data[i].xa = ''
            }
          }
        }
        
        // if(type == '单选'){
        //   if (data[i].cla == name) {
        //     data[i].cla = ''
        //   } else {
        //     data[i].cla = name
        //   }
        // } 
        // if(type == '多选'){
        //   if(data[i].you_option.length > 0){
        //     data[i].you_option = data[i].you_option + '/' + name
        //   }else{
        //     data[i].you_option = name
        //   }
        // }
      }
    }
    that.storage(data,that);
    if(type == '单选'){
      setTimeout(function () {
        if (index < data.length - 1) {
          that.setData({
            num: index + 1
          })
        }
      }, 300)
    }
    
    
    
    // 跳转下一题
    // setTimeout(function () {
    //   that.next(index, that);
    // },1000)
  },
  
  bindconfirm:function(e){
    var that = this;
    var value = e.detail.value;
    var index = e.currentTarget.dataset.index;
    var data = that.data.course;
    for (var i = 0; i < data.length; i++) {
      if (i == index) {
        data[i].you_option = value
      }
    }
    that.storage(data, that);
    setTimeout(function () {
      if (index < data.length - 1) {
        that.setData({
          num: index + 1
        })
      }
    }, 300)
    // 跳转下一题
    // setTimeout(function () {
    //   that.next(index, that);
    // }, 1000)
  },
  //上一题
  // prev:function(e){
  //   // console.log(e);
  //   var that = this;
  //   var id = e.currentTarget.dataset.id; //获取下标
  //   if(id > 0){
  //     id -=1
  //   }
  //   // console.log(id)
    
  //   var course = that.data.course;
  //   for (var i = 0; i < course.length; i++) {
  //     if (i == id) {
  //       course[i].active = true;
  //     } else {
  //       course[i].active = false;
  //     }
  //   }
  //   this.setData({
  //     course: course,
  //   });

  // },
  
  // //下一题
  // next: function (e,that) {
  //   // console.log(e);
  //   var course = that.data.course;
  //   if (e < course.length-1){
  //     e += 1;
  //   }
  //   for (var i = 0; i < course.length; i++) {
  //     if (i == e) {
  //       course[i].active = true;
  //     } else {
  //       course[i].active = false;
  //     }
  //   }
  //   that.setData({
  //     timing:true
  //   })
  //   that.storage(course, that);
  //   that.timing(0,that);
  // },
  // 收藏
  collection:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    var course = that.data.course;
    var sucFunc = function (that, res) {
      //console.log(res.data)
      //that.storage(res.data, that)
    }
    var lianxi = app.d.lianxi == 3 ? 'questionbank_id' :'paperquestions_id'
    var data = { id: id ,openid:app.d.openid,lianxi:lianxi}
    utils.wxRequest(that, app.d.path, "Profession/collection", data, sucFunc);
    for (var i = 0; i < course.length; i++) {
      if (i == index) {
        if (course[i].collection){
          course[i].collection = false;
        }else{
          course[i].collection = true;
        }
      } 
    }
    that.storage(course, that);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    var that = this;
    // var pages = getCurrentPages();
    // var currentPage = pages[pages.length - 1];
    // var url = currentPage.route;
    // var options = currentPage.options;
    
    // 获取高度
    wx.getSystemInfo({
      success(res) {
        that.setData({
          windowHeight: res.windowHeight
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  //切换
  changeMenu: function (e) {
    var that = this
    that.setData({
      currid: e.currentTarget.dataset.id,
      id: e.currentTarget.dataset.id,
      menu_name: e.currentTarget.dataset.name,
      fixedbg: false,
      poptype: false,
    })
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 答题卡
  answer:function(){
    var that = this;
    wx.redirectTo({
      url: '/pages/answer/answer?id=' + that.data.id
    })
  },
  // 交卷
  jiao:function(){
    var that = this;
    app.d.jiao = true
    wx.redirectTo({
      url: '/pages/finish/finish'
    })
  },
 
})